package com.itheima.edu.info.maneger.service;

import com.itheima.edu.info.maneger.dao.BaseTeacherDao;
import com.itheima.edu.info.maneger.dao.TeacherDao;
import com.itheima.edu.info.maneger.domain.Teacher;
import com.itheima.edu.info.maneger.factory.SystemFactory;

public class TeacherService {
    //获取TeacherDao对象
    //TeacherDao teacherDao = new TeacherDao();
    BaseTeacherDao teacherDao = SystemFactory. getTeacherDao();

    public boolean addTeacher(Teacher teacher) {
        //将接受到的学生对象传递给StudentDao 接受boolean类型的返回值
        boolean flag = teacherDao.addTeacher(teacher);
        return flag;
    }

    //判断ID是否存在
    public boolean isExists(String id) {
        Teacher[] teacher = teacherDao.findAllTeacher();
        boolean exit = false;
        for (int i = 0; i < teacher.length; i++) {
            //teacher[i]
            if (teacher[i] != null && teacher[i].getId().equals(id)) {
                exit = true;
            }
        }
        return exit;

    }

    //查看学生方法
    public Teacher[] findAllTeacher() {
        //从StudentDao中获取储存学生对象的数组
        Teacher[] teachers = teacherDao.findAllTeacher();
        boolean flag = false;
        //遍历数组，判断数组是否为空
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            //有信息
            return teachers;
        } else {
            //无信息
            return null;
        }
    }

    //删除学生方法
    public void deleteTeacher(String id) {
        teacherDao.deleteTeacher(id);
    }


    public void updateTeacher(String updateId, Teacher newteacher) {
        teacherDao.updateTeacher(updateId, newteacher);
    }
}
